﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zo.Extensions;
using Zo.Models;
using ZoLone.Models.Sys;

namespace ZoLone.Logics.Sys
{
    public class RoleLogic : BaseLogic
    {
        /// <summary>
        /// 获取权限字符串列表
        /// </summary>
        /// <param name="ids">角色组</param>
        /// <returns></returns>
        public List<int> GetOperationListById(List<int> ids)
        {
            if (ids==null||ids.Count == 0)
            {
                return null;
            }

            var opList = db.Sys_Role.Where(c => ids.Contains(c.Id)).Select(c => c.OperationList);

            List<int> all = new List<int>();
            foreach (var item in opList)
            {
                if (string.IsNullOrEmpty(item))
                {
                    break;
                }
                var xx=item.Split(",");
                foreach (var id in xx)
                {
                    all.Add(Convert.ToInt32(id));
                }
            }
            all.Distinct();
            return all;
        }

        /// <summary>
        ///返回权限组
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<int> GetOperationListById(int roleId)
        {
            List<int> result = new List<int>();
            if (roleId == 0 || roleId == 1)
            {
                return result;
            }

            var opList = db.Sys_Role.Where(c => c.Id == roleId).Select(c => c.OperationList).FirstOrDefault();
            if (string.IsNullOrEmpty(opList))
            {
                return result;
            }

            var rs = opList.Split(',');
            if (rs.Length > 0)
            {

                foreach (var item in rs)
                {
                    result.Add(Convert.ToInt32(item));
                }

            }
            return result;

        }

        /// <summary>
        /// 获取所有的角色
        /// </summary>
        /// <returns></returns>
        public List<Sys_Role> GetAllRoles()
        {
            var list = db.Sys_Role.ToList();
            return list;
        }

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="ids">权限组</param>
        /// <param name="roleId">角色编号</param>
        /// <returns></returns>
        public bool SaveRoleOperation(int[] ids, int roleId)
        {
            string op = string.Join(",", ids);
            var o = db.Sys_Role.Where(c => c.Id.Equals(roleId)).FirstOrDefault();
            if (o == null)
            {
                throw new ErrorBox("The role does not exist");
            }
            o.OperationList = op;
            return db.SaveChanges() > 0;
        }

        public Sys_Role GetRoleInfoById(int roleId)
        {
            var o = db.Sys_Role.Where(c => c.Id.Equals(roleId)).FirstOrDefault();
            if (o == null)
            {
                throw new ErrorBox("The role does not exist");
            }
            return o;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool Save(Sys_Role info)
        {
            if (info.Id > 0)
            {
                var role = db.Sys_Role.Where(c => c.Id.Equals(info.Id)).FirstOrDefault();
                if (db.Sys_Role.Where(c => c.Name.Equals(info.Name) && c.Id != info.Id).FirstOrDefault() != null)
                {
                    throw new ErrorBox("This role's name already exists");
                }
                role.Name = info.Name.Trim();

                role.Remark = info.Remark;
                return db.SaveChanges() > 0;
            }
            else
            {
                info.Name = info.Name.Trim();
                if (db.Sys_Role.Where(c => c.Name.Equals(info.Name)).FirstOrDefault() != null)
                {
                    throw new ErrorBox("This role's name already exists");
                }
                info.IsDelete = 0;
                info.CreateTime = DateTime.UtcNow.GetTimeStamp();
                db.Sys_Role.Add(info);
                return db.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool Delete(int roleId)
        {
            var role = db.Sys_Role.Where(c => c.Id.Equals(roleId)).SingleOrDefault();
            if (role == null)
            {
                throw new ErrorBox("This role does not exist!");
            }
            if (roleId == 1)
            {
                throw new ErrorBox("Super admin's role cannot  delete!");
            }
            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    db.Sys_Role.Remove(role);
                    //同时删除使用该角色的管理员关系
                    db.Sys_AdminRole.RemoveRange(db.Sys_AdminRole.Where(c=>c.RoleId.Equals(roleId)));
                    var rr = db.SaveChanges();
                    tran.Commit();
                    return rr > 0;
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    throw new ErrorBox(e.Message);
                }


            }

        }

    }
}
